package com.missilike.api.controller;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.missilike.core.enums.SmsType;
import com.missilike.core.result.BizResultCode;
import com.missilike.core.result.CommonResult;
import com.missilike.core.service.ISmsService;
import com.missilike.core.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2018-12-10 22:49
 **/
@Slf4j
@RestController
@RequestMapping("/api/sms")
public class SmsController {

    private ISmsService smsService;

    public SmsController(ISmsService smsService) {
        this.smsService = smsService;
    }

    @ApiOperation(value = "sendLoginCode", notes = "发送登录验证码")
    @RequestMapping(value = "/sendLoginCode", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult sendLoginCode(@RequestParam String phoneNumber) throws ClientException {
        log.info("Start to execute /sms/sendLoginCode body request={}", phoneNumber);
        SendSmsResponse sendSmsResponse = smsService.sendCode(phoneNumber, SmsType.LOGIN);
        log.info("Start to execute /sms/sendLoginCode responese={}", JSON.toJSON(sendSmsResponse));
        return sendSmsResponse.getMessage().equals("OK") ? ResultUtils.ok() : ResultUtils.error(BizResultCode.SEND_VERIFICATION_CODE_FAIL);
    }

    @ApiOperation(value = "sendRegisterCode", notes = "发送注册验证码")
    @RequestMapping(value = "/sendRegisterCode", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult sendRegisterCode(@RequestParam String phoneNumber) throws ClientException {
        log.info("Start to execute /sms/sendRegisterCode body request={}", phoneNumber);
        SendSmsResponse sendSmsResponse = smsService.sendCode(phoneNumber, SmsType.REGISTER);
        log.info("Start to execute /sms/sendRegisterCode responese={}", JSON.toJSON(sendSmsResponse));
        return sendSmsResponse.getMessage().equals("OK") ? ResultUtils.ok() : ResultUtils.error(BizResultCode.SEND_VERIFICATION_CODE_FAIL);
    }

    @ApiOperation(value = "sendResetCode", notes = "发送重置验证码")
    @RequestMapping(value = "/sendResetCode", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult sendResetCode(@RequestParam String phoneNumber) throws ClientException {
        log.info("Start to execute /sms/sendResetCode body request={}", phoneNumber);
        SendSmsResponse sendSmsResponse = smsService.sendCode(phoneNumber, SmsType.RESET);
        log.info("Start to execute /sms/sendResetCode responese={}", JSON.toJSON(sendSmsResponse));
        return sendSmsResponse.getMessage().equals("OK") ? ResultUtils.ok() : ResultUtils.error(BizResultCode.SEND_VERIFICATION_CODE_FAIL);
    }

    @ApiOperation(value = "verificationRegisterCode", notes = "校验注册验证码")
    @RequestMapping(value = "/verificationRegisterCode", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult verificationRegisterCode(@RequestParam String phoneNumber, String code) {
        log.info("Start to execute /sms/verificationRegisterCode body request={}", phoneNumber, code);
        Boolean result = smsService.verificationCode(phoneNumber, code, SmsType.REGISTER);
        log.info("Start to execute /sms/verificationRegisterCode responese={}", JSON.toJSON(result));
        return result ? ResultUtils.ok() : ResultUtils.error(BizResultCode.VERIFICATION_CODE_CHECK_FAIL);
    }

    @ApiOperation(value = "verificationResetCode", notes = "校验重置验证码")
    @RequestMapping(value = "/verificationResetCode", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult verificationResetCode(@RequestParam String phoneNumber, String code) {
        log.info("Start to execute /sms/verificationResetCode body request={}", phoneNumber, code);
        Boolean result = smsService.verificationCode(phoneNumber, code, SmsType.RESET);
        log.info("Start to execute /sms/verificationResetCode responese={}", JSON.toJSON(result));
        return result ? ResultUtils.ok() : ResultUtils.error(BizResultCode.VERIFICATION_CODE_CHECK_FAIL);
    }
}
